
package com.joysuch.wwyt.datadump.biz.convertor;

import java.util.Optional;

import cn.zlg.common.datadump.annotation.DataDumpField;
import cn.zlg.common.datadump.convertor.FieldValueConvert;
import com.joysuch.wwyt.bp.entity.contractor.BpContractorStaff;
import com.joysuch.wwyt.bp.repository.BpContractorStaffDao;
import com.joysuch.wwyt.core.entity.BaseUser;
import com.joysuch.wwyt.core.repository.BaseUserDao;
import com.joysuch.wwyt.edu.entity.dsl.QEduCertificateUser;
import com.querydsl.core.Tuple;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 根据人员类型，查找BaseUser或BpContractorStaff的人员信息
 * 
 * @since
 *
 */
@Component
public class ExportEduCertificateUserInfoConvertor implements FieldValueConvert<Long, Object> {

    private static final Logger logger = LoggerFactory.getLogger(ExportEduCertificateUserInfoConvertor.class);

    @Autowired
    private BaseUserDao baseUserDao;
    @Autowired
    private BpContractorStaffDao bpContractorStaffDao;

    @Override
    public Object convert(Object bean, Long userId, DataDumpField dataField) {
        if (userId == null) {
            return null;
        }
        String property = dataField.exportConvertorParams()[0];
        Integer flag = ((Tuple) bean).get(QEduCertificateUser.eduCertificateUser.userContractorFlag);
        if (flag != null) {
            if (flag == 0) {
                Optional<BaseUser> user = baseUserDao.findById(userId);
                if (user.isPresent()) {
                    if ("code".equals(property)) {
                        return user.get().getCode();
                    } else {
                        return user.get().getRealName();

                    }
                }
            } else if (flag == 1) {
                Optional<BpContractorStaff> user = bpContractorStaffDao.findById(userId);
                if (user.isPresent()) {
                    if ("code".equals(property)) {
                        return user.get().getCode();
                    } else {
                        return user.get().getName();

                    }
                }
            }
        }
        return null;
    }

}
